﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PIT.Labs.SharePoint.Extensions
{
    /// <summary>
    /// This class contains "<a href="http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.90%29.aspx">Extension Methods</a>" for the <see cref="T:Microsoft.SharePoint.SPWeb"/> class.
    /// </summary>
    /// <seealso cref="T:Microsoft.SharePoint.SPWeb"/>
    public static class SPWebExtension
    {
        /// <summary>
        /// This method checks if a SharePoint list is existing. This method uses elevated rights, so maybe the list is existing, but the user cannot see it.
        /// </summary>
        /// <param name="website">The current SPWeb instance</param>
        /// <param name="listID">The identifier of the list</param>
        /// <returns><see cref="Boolean" /><br /><b>true</b> if the list is existing; otherwise <b>false</b></returns>
        [CLSCompliantAttribute(false)]
        public static bool ContainsList(this SPWeb website, Guid listID)
        {
            bool isExisting = false;
            using (SPSite siteCollection = new SPSite(website.Url))
            {
                using (SPSite elevatedSite = siteCollection.GetElevatedSite())
                {
                    using (SPWeb web = elevatedSite.OpenWeb(website.ID))
                    {
                        try
                        {
                            SPList tmp = web.Lists[listID];
                            if (tmp != null)
                                isExisting = true;
                        }
                        catch (SPException ex)
                        {
                            //Only 
                            //COMException (0x81020026): List does not exist
                            //is caught
                            if (ex.ErrorCode != -2130575322)
                                throw;
                        }
                    }
                }
            }
            return isExisting;
        }

        /// <summary>
        /// Get a list by its internal name.
        /// </summary>
        /// <param name="website">Represents a SharePoint Foundation spGroup.</param>
        /// <param name="internalName">The internal name of a list (root folder).</param>
        /// <returns>
        /// <see cref="T:Microsoft.SharePoint.SPList"/>
        /// The corresponding list, if it can be found, otherwise <b>NULL</b>.
        /// </returns>
        [CLSCompliantAttribute(false)]
        public static SPList GetListByInternalName(this SPWeb website, String internalName)
        {
            return website.Lists.Cast<SPList>().FirstOrDefault(l => l.RootFolder.Name == internalName);
        }

        /// <summary>
        /// Updates the database with changes that are made to the spGroup. Allows updates via HTTP-GET (Caution: XSS-Attacks)!
        /// </summary>
        /// <param name="website">Represents a SharePoint Foundation spGroup.</param>
        [CLSCompliantAttribute(false)]
        public static void UnsafeUpdate(this SPWeb website)
        {
            if (website != null)
            {
                bool oldState = website.AllowUnsafeUpdates;
                website.AllowUnsafeUpdates = true;
                website.Update();
                website.AllowUnsafeUpdates = oldState;
            }
        }

        /// <summary>
        /// Checks, if a particular group is existing in the root web (site collection level).
        /// </summary>
        /// <param name="website">Represents a SharePoint Foundation spGroup.</param>
        /// <param name="name">The name of the group.</param>
        /// <returns><see cref="Boolean"/><br/>
        /// <b>true</b>, if the group is existent in the root web, otherwise <b>false</b>.
        /// </returns>
        [CLSCompliantAttribute(false)]
        public static bool ContainsSiteGroup(this SPWeb website, String name)
        {
            bool isExisting = false;
            if (website != null && !String.IsNullOrEmpty(name))
            {
                try
                {
                    SPGroup siteGroups = website.SiteGroups[name];
                    isExisting = true;
                }
                catch (SPException ex)
                {
                    if (ex.ErrorCode != -2146232832)
                        throw;
                }
            }
            return isExisting;
        }
    }
}
